package com.slaffka.services;

import com.slaffka.model.user.User;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by IntelliJ IDEA.
 * UserService: v.tsapaev
 * Date: 22.02.12
 * Time: 14:54
 * To change this template use File | Settings | File Templates.
 */
@Service
public class UserService {

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public User getUserByUsername(String username){
        return (User)sessionFactory.getCurrentSession()
                .createQuery("from User where username = :username")
                .setParameter("username", username)
                .uniqueResult();
    }

    @Transactional
    public User createUser(User user){
//        User entityUser = getUserByUsername(user.getUsername());
//        if (entityUser != null)
//        {
//            return entityUser;
//        }
        sessionFactory.getCurrentSession().save(user);
        return user;
    }
}
